﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MusicService
{
    public partial class MusicService:ITrackService
    {
        public bool addTrack(string songID, string trackURL, DateTime createDate, long totalPlay)
        {
            bool flag = false;
            Track track = new Track();
            try
            {
                long numberTrack = dc.Tracks.Count() + 1;
                track.TrackID = "TRACK" + numberTrack;
                track.SongID = songID;
                track.TrackURL = trackURL;
                track.CreateDate = createDate;
                track.TotalPlay = totalPlay;
                dc.Tracks.InsertOnSubmit(track);
                dc.SubmitChanges();
                flag = true;
            }
            catch (Exception)
            {

            }
            return flag;
        }

        public bool removeTrack(string trackID)
        {
            bool flag = false;
            try
            {
                Track track = dc.Tracks.Single(t => t.TrackID.Equals(trackID));
                if (track != null)
                {
                    dc.Tracks.DeleteOnSubmit(track);
                    dc.SubmitChanges();
                    flag = true;
                }
            }
            catch (Exception)
            {

            }
            return flag;
        }

        public bool updateTrack(string trackID, string songID, string trackURL, DateTime createDate, long totalPlay)
        {
            bool flag = false;
            try
            {
                Track track = dc.Tracks.Single(t => t.TrackID.Equals(trackID));
                if (track != null)
                {
                    track.SongID = songID;
                    track.TrackURL = trackURL;
                    track.CreateDate = createDate;
                    track.TotalPlay = totalPlay;
                    dc.Tracks.InsertOnSubmit(track);
                    dc.SubmitChanges();
                    flag = true;
                }
            }
            catch (Exception)
            {

            }
            return flag;
        }

        public List<Track> getAllTrack()
        {
            var list = from t in dc.Tracks select t;
            return list.ToList();
        }

        public Track getTrackByTrackID(string trackID)
        {
            Track track = dc.Tracks.Single(t => t.TrackID.Equals(trackID));
            return track;
        }
    }
}